/*
 * ShopInfoFragment.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.shopdetail.info;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.manager.ImageManager;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.ui.widget.dialog.CallPhoneDialog;
import com.yidiandao.business.ui.widget.recyclerview.InnerLinearLayoutManager;
import com.yidiandao.business.ui.widget.recyclerview.provider.ShopInfoImageProvider;
import com.yidiandao.business.ui.widget.view.RatioLayout;
import com.yidiandao.business.ui.widget.view.SpaceItemDecoration;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.LocationChooseUtils;
import com.yidiandao.business.utils.UIUtils;
import com.yidiandao.business.utils.UmengUtils;

import java.util.List;

import me.drakeet.multitype.MultiTypeAdapter;

/** 店铺详情-店铺信息-View */
public class ShopInfoFragment extends BaseFragment
        implements ShopInfoContract.View, View.OnClickListener {

    private static final String TAG = ShopInfoFragment.class.getSimpleName();

    private ShopInfoContract.Presenter mPresenter;

    private ImageView mIcActive;

    private LinearLayout mLlytShopImages;

    private RecyclerView mRecyclerView;

    private TextView mTvShopInfo;

    private LinearLayout mRlytPhone;

    private TextView mTvPhone;

    private LinearLayout mRlytQq;

    private TextView mTvServiceQq;

    private LinearLayout mRlytEmail;

    private TextView mTvEmail;

    private LinearLayout mRlytUrl;

    private TextView mTvUrl;

    private TextView mTvAddress;

    private String mAdtiveUrl;

    private LinearLayout mRlytAddress;

    private RatioLayout mRatioLayout;

    private MultiTypeAdapter mShopImageAdapter;

    private String mInformation;

    private LocationChooseUtils mLocationChooseUtils;

    public static ShopInfoFragment getInstance(String supplierId) {
        ShopInfoFragment fragment = new ShopInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString(Intents.EXTRA_SUPPLIER_ID, supplierId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initViews() {
        mRatioLayout = (RatioLayout) mRootView.findViewById(R.id.ratio_layout);
        mIcActive = (ImageView) mRootView.findViewById(R.id.ic_active);
        mLlytShopImages = (LinearLayout) mRootView.findViewById(R.id.llyt_shop_images);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);
        mTvShopInfo = (TextView) mRootView.findViewById(R.id.tv_shop_info);
        mRlytPhone = (LinearLayout) mRootView.findViewById(R.id.rlyt_phone);
        mTvPhone = (TextView) mRootView.findViewById(R.id.tv_phone);
        mRlytQq = (LinearLayout) mRootView.findViewById(R.id.rlyt_qq);
        mTvServiceQq = (TextView) mRootView.findViewById(R.id.tv_service_qq);
        mRlytEmail = (LinearLayout) mRootView.findViewById(R.id.rlyt_email);
        mTvEmail = (TextView) mRootView.findViewById(R.id.tv_email);
        mRlytUrl = (LinearLayout) mRootView.findViewById(R.id.rlyt_url);
        mTvUrl = (TextView) mRootView.findViewById(R.id.tv_url);
        mRlytAddress = (LinearLayout) mRootView.findViewById(R.id.rlyt_address);
        mTvAddress = (TextView) mRootView.findViewById(R.id.tv_address);

        initRecyclerView();
        initListeners();
        mPresenter.extraBundle(getArguments());
        mPresenter.start();
    }

    private void initListeners() {
        mIcActive.setOnClickListener(this);
        mRlytPhone.setOnClickListener(this);
        mRlytQq.setOnClickListener(this);
        mRlytEmail.setOnClickListener(this);
        mRlytUrl.setOnClickListener(this);
        mRlytAddress.setOnClickListener(this);
    }

    private void initRecyclerView() {
        mShopImageAdapter = new MultiTypeAdapter();
        InnerLinearLayoutManager layoutManager = new InnerLinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        mShopImageAdapter.register(String.class, new ShopInfoImageProvider(getActivity()));

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(
                new SpaceItemDecoration(0, 0, (int) UIUtils.getDimens(R.dimen.dimen_5), 0));
        mRecyclerView.setAdapter(mShopImageAdapter);

        mRecyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shop_info;
    }

    @Override
    public void setPresenter(ShopInfoContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    public void refreshShopInfo(
            String shopBigAd,
            String shopAdImage,
            String information,
            String address,
            String tel,
            String qq,
            String url,
            String email) {
        mAdtiveUrl = shopAdImage;
        mInformation = information;
        mRatioLayout.setVisibility(TextUtils.isEmpty(shopBigAd) ? View.GONE : View.VISIBLE);
        if (!TextUtils.isEmpty(shopBigAd)) {
            ImageLoader.loadImage(mActivity, mIcActive, shopBigAd);
        }
        mTvShopInfo.setText(UIUtils.getString(R.string.shop_info_tab, information));
        mTvAddress.setText(
                TextUtils.isEmpty(address) ? UIUtils.getString(R.string.shop_info_null) : address);
        mTvPhone.setText(TextUtils.isEmpty(tel) ? UIUtils.getString(R.string.shop_info_null) : tel);
        mTvServiceQq.setText(
                TextUtils.isEmpty(qq) ? UIUtils.getString(R.string.shop_info_null) : qq);
        mTvEmail.setText(
                TextUtils.isEmpty(email) ? UIUtils.getString(R.string.shop_info_null) : email);
        mTvUrl.setText(TextUtils.isEmpty(url) ? UIUtils.getString(R.string.shop_info_null) : url);
    }

    @Override
    public void showShopImagesLayout(boolean show) {
        mLlytShopImages.setVisibility(show ? View.VISIBLE : View.GONE);
        if (!show && TextUtils.isEmpty(mInformation)) {
            mLlytShopImages.setVisibility(View.GONE);
        }
    }

    @Override
    public void refreshShopImages(List<String> pictures) {
        ImageManager.getInstance().setCurrentImageList(pictures);
        mShopImageAdapter.setItems(pictures);
        mShopImageAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ic_active:
                UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_invite);
                UrlActivity.actionStart(mActivity, mAdtiveUrl);
                break;
            case R.id.rlyt_qq:
                // 打开QQ
                String qq = mTvServiceQq.getText().toString().trim();
                if (!qq.equals(UIUtils.getString(R.string.shop_info_null))) {
                    UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_qq);
                    toQQActivity(qq);
                }
                break;
            case R.id.rlyt_phone:
                // 打开电话
                String tel = mTvPhone.getText().toString().trim();
                if (!tel.equals(UIUtils.getString(R.string.shop_info_null))) {
                    UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_call);
                    showCallPhoneDialog(tel);
                }

                break;
            case R.id.rlyt_email:
                // 打开邮箱
                String email = mTvEmail.getText().toString().trim();
                if (!email.equals(UIUtils.getString(R.string.shop_info_null))) {
                    UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_email);
                    toEmailActivity(email);
                }
                break;
            case R.id.rlyt_url:
                // 打开网址
                String url = mTvUrl.getText().toString().trim();
                if (!url.equals(UIUtils.getString(R.string.shop_info_null))) {
                    UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_website);
                    toUrlActivity(url);
                }
                break;
            case R.id.rlyt_address:
                // 打开地址导航
                final String address = mTvAddress.getText().toString().trim();
                if (!TextUtils.isEmpty(address)) {
                    UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_maps);
                    if (null == mLocationChooseUtils) {
                        mLocationChooseUtils = new LocationChooseUtils();
                    }
                    mLocationChooseUtils.showGuideDialog(mActivity, address);
                }
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        UmengUtils.onEvent(mActivity, R.string.umeng_shop_detail_info);
    }

    private void showCallPhoneDialog(String tel) {
        CallPhoneDialog dialog = new CallPhoneDialog(mActivity);
        dialog.setPhoneNum(tel);
        dialog.show();
    }

    @Override
    public void toUrlActivity(String url) {
        try {
            //调用系统默认浏览器
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        } catch (Exception e) {
            //调用WebView打开
            if (!url.contains("http://")) url = "http://" + url;
            if (url.contains("https://")) url.replace("https://", "http://");
            UrlActivity.actionStart(mActivity, url);
            e.printStackTrace();
        }
    }

    @Override
    public void toEmailActivity(String email) {
        try {
            Uri uri = Uri.parse("mailto:" + email);
            Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
            startActivity(intent);
        } catch (Exception e) {
            showToast(UIUtils.getString(R.string.shop_info_uninstall_email));
            e.printStackTrace();
        }
    }

    @Override
    public void toQQActivity(String qq) {
        try {
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=" + qq + "&version=1";
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        } catch (ActivityNotFoundException e) {
            showToast(UIUtils.getString(R.string.shop_info_uninstall_qq));
        }
    }
}
